﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Structs;
using System.IO;

namespace MemoryController
{
    public class MemoryWriter
    {
        private static string abc = "0123456789АБВГДЕЁЖЗИЙКЛМНОПРСТУФХЦЧШЩЬЫЪЭЮЯQWERTYUIOPASDFGHJKLZXCVBNM";
        private static string Rootpath = string.Empty;

        public static void CreateDirs(string rootpath)
        {
            Rootpath = rootpath;
            string path = string.Empty;
            for (int i = 0; i < abc.Length; i++)
            {
                path = "\\Memory\\" + abc[i];
                Directory.CreateDirectory(rootpath + path);
            }
        }

        public static void RememberWord(Word word)
        {
            string path = "\\Memory\\" + word.Norm[0] + "\\" + word.Norm + ".txt";
            FileStream fs = new FileStream(Rootpath + path, FileMode.OpenOrCreate);
            StreamWriter sw = new StreamWriter(fs, Encoding.GetEncoding("Windows-1251"));
            for (int i = 0; i < word.Words.Count; i++)
                sw.WriteLine(word.Words[i].Form);
            sw.Close();
            fs.Close();
        }

        public static void RememberWords(List<Word> words)
        {
            for (int i = 0; i < words.Count - 1; i++)
            {
                RememberWord(words[i]);
                if (words[i].Words.Count != 0)
                    RememberWords(words[i].Words);
            }
        }
    }
}
